package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Foodtype;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("FoodtypeDAO")
@Transactional
public class FoodtypeDAOImpl extends AbstractJpaDao<Foodtype> implements FoodtypeDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Foodtype.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public FoodtypeDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Foodtype findFoodtypeById(Integer id) throws DataAccessException {

		return findFoodtypeById(id, -1, -1);
	}

	@Transactional
	public Foodtype findFoodtypeById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodtypeById", startResult, maxRows, id);
			return (Foodtype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodtype> findFoodtypeByName(String name) throws DataAccessException {

		return findFoodtypeByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodtype> findFoodtypeByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFoodtypeByName", startResult, maxRows, name);
		return new LinkedHashSet<Foodtype>(query.getResultList());
	}

	@Transactional
	public Set<Foodtype> findFoodtypeByNameContaining(String name) throws DataAccessException {

		return findFoodtypeByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodtype> findFoodtypeByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodtypeByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Foodtype>(query.getResultList());
	}

	@Transactional
	public Set<Foodtype> findFoodtypeByDescription(String description) throws DataAccessException {

		return findFoodtypeByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodtype> findFoodtypeByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodtypeByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Foodtype>(query.getResultList());
	}

	@Transactional
	public Set<Foodtype> findFoodtypeByDescriptionContaining(String description) throws DataAccessException {

		return findFoodtypeByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodtype> findFoodtypeByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodtypeByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Foodtype>(query.getResultList());
	}

	@Transactional
	public Foodtype findFoodtypeByPrimaryKey(Integer id) throws DataAccessException {
		return findFoodtypeByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Foodtype findFoodtypeByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodtypeByPrimaryKey", startResult, maxRows, id);
			return (Foodtype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodtype> findFoodtypesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodtypesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodtype> findAllFoodtypes() throws DataAccessException {

		return findAllFoodtypes(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodtype> findAllFoodtypes(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodtypes", startResult, maxRows);
		return new LinkedHashSet<Foodtype>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodtype> findFoodtypesByParent(Integer foodtypeId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodtypesByParent", start, max, foodtypeId);
		return new LinkedHashSet<Foodtype>(query.getResultList());
	}

	@Override
	public Set<Integer> findFoodtypeIdsByParent(int rootFoodtypeId) throws DataAccessException {
		Query query = createNamedQuery("findFoodtypeIdsByParent", -1, -1, rootFoodtypeId);
		return new LinkedHashSet<Integer>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodtype> findFoodtypeByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Foodtype>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Foodtype> findFoodtypesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Foodtype.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Foodtype> foodtypes = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return foodtypes;
	}

	@Transactional
	public Foodtype findFoodtypeByPhysicalPrimaryKey(String name, Integer parentId) throws DataAccessException {
		return findFoodtypeByPhysicalPrimaryKey(name, parentId, -1, -1);
	}

	@Transactional
	public Foodtype findFoodtypeByPhysicalPrimaryKey(String name, Integer parentId, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodtypeByPhyscialPrimaryKey", startResult, maxRows, name, parentId);
			return (Foodtype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Foodtype entity) {
		return true;
	}
}
